package org.openkoala.koala.popup.javaclass.crud;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.openkoala.koala.deploy.curd.module.core.EntityModel;
import org.openkoala.koala.deploy.curd.module.ui.Model;
import org.openkoala.koala.deploy.curd.module.ui.model.ViewModel;

/**
 * 
 * 类    名：QueryControl.java
 *   
 * 功能描述：具体功能做描述。	
 *  
 * 创建日期：2013-1-24下午2:41:17     
 * 
 * 版本信息：
 * 
 * 版权信息：Copyright (c) 2013 Koala All Rights Reserved
 * 
 * 作    者：xmfang(xinmin.fang@gmail.com)
 * 
 * 修改记录： 
 * 修 改 者    修改日期     文件版本   修改说明
 */
public class CrudTabControl {
    
    private Composite parent;
    
    private Model model;
    private EntityModel entityModel;
    
    CrudTabControl(Composite parent, Model model, EntityModel entityModel) {
        this.parent = parent;
        this.model = model;
        this.entityModel = entityModel;
    }
    
    public Control getControl() {
        GridData gridData = new GridData(GridData.FILL_BOTH);
        GridLayout gridLayout = new GridLayout(1, false);
        
        Composite composite = new Composite(parent, SWT.NONE);
        composite.setLayoutData(gridData);
        composite.setLayout(gridLayout);
        
        CrudConfigurationComponent crudConfigurationComponent = new CrudConfigurationComponent(composite, model, entityModel);
        crudConfigurationComponent.createControl();
        
        if (model instanceof ViewModel) {
        	ViewModel viewModel = (ViewModel) model;
        	if (viewModel.getTabs() != null) {
        		generateRelationModelComposite(composite, (ViewModel) model);
        	}
        }
        
        return composite;
    }

    private void generateRelationModelComposite(Composite parent, ViewModel viewModel) {
        GridData gridData = new GridData(GridData.FILL_BOTH);
        GridLayout gridLayout = new GridLayout(1, false);
        
        Composite composite = new Composite(parent, SWT.NONE);
        composite.setLayoutData(gridData);
        composite.setLayout(gridLayout);
        
    	RelationViewComponent relationViewComponent = new RelationViewComponent(composite, viewModel, entityModel);
    	relationViewComponent.createControl();
    }
    
//    private void generateRelationModelComposite(Composite parent, ViewModel viewModel) {
//        GridData gridData = new GridData(GridData.FILL_BOTH);
//        GridLayout gridLayout = new GridLayout(1, false);
//        
//        Composite composite = new Composite(parent, SWT.NONE);
//        composite.setLayoutData(gridData);
//        composite.setLayout(gridLayout);
//
//        TabFolder tabFolder = new TabFolder(composite, SWT.NONE);
//	    tabFolder.setLayoutData(gridData);
//        
//	    for (TabViewUI tabViewUI : viewModel.getTabs()) {
//	    	TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
//	    	tabItem.setText(tabViewUI.getRelationFieldModel().getName());
//
//	    	CrudConfigurationComponent crudConfigurationComponent = new CrudConfigurationComponent(tabFolder, tabViewUI.getListModel());
//	    	tabItem.setControl(crudConfigurationComponent.createControl());
//	    }
//	    
//    }
    
}
